package com.kingsoft.dc.khaos.module.spark.model.TechCheck;

import java.util.regex.Pattern;

public class KsyunStringCheckRule extends KsyunCheckRule {
    private static final Pattern aPattern = Pattern.compile("^[a-zA-Z]+$");
    private static final Pattern nPattern = Pattern.compile("^[\\d]+$");
    private static final Pattern anPattern = Pattern.compile("^[a-zA-Z\\d]+$");

    private String dataType;
    private int lenth;
    private String dataScope; // datatype去掉长度描述信息
    //是否定长 ： true 定长字符串，false 不定长字符串
    private boolean isfixedlength;
    public KsyunStringCheckRule(String dataType, int lenth, String dataScope , boolean isfixedlength){
        this.dataType = dataType;
        this.lenth = lenth;
        this.dataScope = dataScope;
        this.isfixedlength = isfixedlength;
    }

    @Override
    public Tuple4<String,String, KsyunErrorCode,String> check(String value) {
        if(isfixedlength){
            //定长字符串
            if(value.length() != lenth)
                throw new KsyunTechCheckException(KsyunErrorCode.STRING_FIX_LENG ,"规则："+dataType+ "实际长度：" + value.length() );
        }else{
            //不定长字符串
            if(value.length()>lenth)
                throw new KsyunTechCheckException(KsyunErrorCode.STRING_NOTFIX_LENG, "规则："+dataType+ "实际长度：" + value.length() );
        }
        //空字符串不做校验
        if(value.trim().length()==0) //空字符串不做校验
            return null;
        if(dataScope.equals("a") && !aPattern.matcher(value.trim()).matches() )
            return new Tuple4(value,null, KsyunErrorCode.STRING_A_CHECK,"取值范围[a-zA-Z]"+ "实际值:"+value);
        else if(dataScope.equals("n") && !nPattern.matcher(value.trim()).matches() ){
            return new Tuple4(value,null, KsyunErrorCode.STRING_N_CHECK,"取值范围[0-9]"+ "实际值:"+value);
        }else if(dataScope.equals("an") && !anPattern.matcher(value.trim()).matches() ){
            return new Tuple4(value,null, KsyunErrorCode.STRING_AN_CHECK,"取值范围[a-zA-Z][0-9]"+ "实际值:"+value);
        }
        return null;
    }

    public static void main(String[] args) {
        String a = "汉字和13";
        System.out.println(a.length());
    }
}
